const app = getApp()
const font = require('../../../lib/js/font.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.height,
    titleBarHeight: app.globalData.titleBarHeight,
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '快速选型', //导航栏 中间的标题
      color: "#2242A8",
      txtColor: '#FFFFFF'
    },
    nowIdx: 0,//当前swiper索引
    region: ['江苏省', '苏州市', '昆山市'],
    address: "",
    provinceId: "",
    province: "",
    cityId: "",
    city: "",
    districtId: "",
    district: "",
    projectType: "",
    projectName: "",
    area: "",
    temperature: "",
    coolingCapacity: "",
    projectList: [],
    isShow:false,
    isSubmit: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    font.loadFont(); //下载字体
    this.getProject()
    // this.getBoxList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
 
  // },
  bindRegionChange: function (e) {
    console.log(e)
    this.setData({
      address: e.detail.value[0] + '-' + e.detail.value[1] + '-' + e.detail.value[2],
      provinceId: e.detail.code[0],
      province: e.detail.value[0],
      cityId: e.detail.code[1],
      city: e.detail.value[1],
      districtId: e.detail.code[2],
      district: e.detail.value[2],
      region: e.detail.value
    })
    this.data.areaName = this.data.region[2]
  },
   //获取个人信息
   getProject(){
     let that = this
    app.fly.get('/ma/box/selection/ProjectTypes')
    .then(function (res) {

      if (res.code === 0) {
         that.setData({
           projectList: res.data
         })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }, function (res) {
      console.log(res);
    });
   },
   getBoxList(){
    let that = this
    app.fly.get('/ma/box/selection/boxes',{
      // power: that.data.coolingCapacity
      power: 34
    })
    .then(function (res) {

      if (res.code === 0) {
       
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }, function (res) {
      console.log(res);
    });
   },
   getShow(){
    this.setData({
      isShow: true
     })
   },
   closeShow(){
    this.setData({
      isShow: false
     })
   },
   choose(e){
     console.log(e)
     this.setData({
      projectType: e.target.dataset.id,
      projectName: e.target.dataset.name, 
      isShow: false
     })
   },
   onArea(event){
    let value = event.detail.value;
    this.setData({ area: value });
   },
   onTemperature(event){
    let value = event.detail.value;
    this.setData({ temperature: value });
   },
   onCoolingCapacity(event){
    let value = event.detail.value;
    this.setData({ coolingCapacity: value });
      if(this.data.coolingCapacity){
        this.setData({
          isSubmit: true
        })
      }else{
        this.setData({
          isSubmit: false
        })
      }
   },
   saveData(){
     let that = this
     if(Number(that.data.coolingCapacity)>8792.5){
      wx.showToast({
        title: '制冷量最大值为8792.5',
        icon: 'none',
        duration: 2000
      })
      return
     }
      app.fly.post('/ma/box/selection/quick',{
        provinceId: that.data.provinceId ? that.data.provinceId : 0,
        province: that.data.province ? that.data.province : 0,
        cityId: that.data.cityId ? that.data.cityId : 0,
        city: that.data.city ? that.data.city : 0,
        districtId: that.data.districtId ? that.data.districtId : 0,
        district: that.data.district ? that.data.district : 0,
        projectType: that.data.projectType ? that.data.projectType : 0,
        area: that.data.area ? that.data.area : 0,
        temperature: that.data.temperature ? that.data.temperature : 0,
        coolingCapacity: that.data.coolingCapacity ? that.data.coolingCapacity : 0,
      })
      .then(function (res) {

        if (res.code === 0) {
           wx.showToast({
            title: '提交成功',
            icon: 'none',
            duration: 2000
          })
          // setTimeout(function (){
          //   wx.navigateTo({
          //     url: '/packageA/pages/detail/detail?id=' + res.data,
          //   })
          // },1500)
          let list = res.data.combination.concat(res.data.single)
          if(list.length>1){
            let str=encodeURIComponent(JSON.stringify(res.data));
            setTimeout(function (){
              wx.navigateTo({
                url: '/packageB/pages/group/group?info=' + str,
              })
            },1500)
          }else{
            setTimeout(function (){
              wx.navigateTo({
                url: '/packageA/pages/detail/detail?id=' + res.data.single[0].id,
              })
            },1500)
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }, function (res) {
        console.log(res);
      });
   },
})